package org.vgr.module.constraints;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.service.ServiceRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class DynamicListConstraint extends ListOfValuesConstraint implements Serializable {

	private static final long serialVersionUID = 6451162899974544346L;
	
	protected static Log logger = LogFactory.getLog(DynamicListConstraint.class);
	
    @Override
    public void initialize() {}

    @Override
    public List<String> getAllowedValues() {
        List<String> allowedValues = this.getResult();
        super.setAllowedValues(allowedValues);
        return allowedValues;
    }

    protected abstract List<String> getResult();

    /**
     * Set the values that are allowed by the constraint.
     * 
     * @param values a list of allowed values
     */
    @Override
	@SuppressWarnings("unchecked")
    public void setAllowedValues(List allowedValues) {
    }

    @Override
    protected void evaluateCollection(Collection<Object> collection) {
    }

    /** Null implementation effectively turns off constraints checking */
    @Override
    protected void evaluateSingleValue(Object value) {
    }

    private static ServiceRegistry registry;

    public ServiceRegistry getServiceRegistry() {
        return registry;
    }

    public void setServiceRegistry(ServiceRegistry registry) {
        DynamicListConstraint.registry = registry;
    }

    protected static String tokenExpression = "\\$\\{[a-zA-Z]+:[a-zA-Z]+\\}";

    public void setTokenExpression(String value) {
        tokenExpression = value;
    }

    public String getTokenExpression() {
        return tokenExpression;
    }
}